using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;

namespace TestProject
{
	public class Genre
	{
		private int m_genre_ID;
		private string m_type;
		
		public Genre ()
		{
		}
		
		public Genre (int genre_ID)
		{
			m_genre_ID = genre_ID;
			getData();
		}
		
		public int genre_ID
		{
			set{
				m_genre_ID = value;
				getData();
			}
			get{
				return m_genre_ID;
			}
		}
		
		public string type
		{
			set{
				m_type = value;
			}
			get{
				return m_type;
			}
		}
		
		private void getData()
		{			
	        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
	        try
			{
				conn.Open();
				string statement = "SELECT type "
					+ "FROM genre WHERE genre_id = '" + this.m_genre_ID + "'";
		        MySqlCommand cmd = new MySqlCommand(statement, conn);			
		        MySqlDataReader reader = cmd.ExecuteReader();
		        while(reader.Read()) {
					m_type = (string) reader["type"];
		        }
				reader.Close();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		    conn.Close();
		}
		
		/// <summary>
		/// Gets all the available genres stored in the database. 
		/// </summary>
		/// <returns>
		/// A <see cref="List<Genre>"/> All the genres in a List<Genre> object.
		/// </returns>
		public static List<Genre> getAllGenres()
		{
	        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString);
			List<Genre> genreList = new List<Genre>(0);
	        try
			{
				conn.Open();
				string statement = "SELECT genre_id from genre";
		        MySqlCommand cmd = new MySqlCommand(statement, conn);			
		        MySqlDataReader reader = cmd.ExecuteReader();
		        while(reader.Read()) {
					genreList.Add(new Genre((int)reader["genre_ID"]));
		        }
				reader.Close();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
		    conn.Close();
			return genreList;
		}
	}
}

